﻿using System;
using System.Linq;
using System.Threading.Tasks;
using Core.Models;
using Service.Model.Context;
using ShortBus;

namespace Service.Workflow.Opening.Query
{
    public class OpeningQueryHandler : IAsyncQueryHandler<OpeningQuery, OpeningQueryResponse>
    {
        private readonly ServiceDbContext dbContext;

        public OpeningQueryHandler(ServiceDbContext serviceDbContext)
        {
            this.dbContext = serviceDbContext;
        }

        public async Task<OpeningQueryResponse> HandleAsync(OpeningQuery command)
        {
            var openings = dbContext.Openings.Where(o => o.FEN.Equals(command.FEN));

            return new OpeningQueryResponse()
            {
                OpeningFound = openings.Any(),
                Moves = openings.Select(o => o.Move).ToList()
            };
        }
    }
}